# -*- coding: utf-8 -*-

'''eof
name:申请企业当前未到期相关还款责任五级分类为关注类笔数
code:EC88_EntCurrRelaGuaranteeConsAccCnt
tableName:
columnName:
groups:行内二代企业征信数据
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import datetime
from dateutil.parser import parse

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def EC88_EntCurrRelaGuaranteeConsAccCnt():
    data = EC_DSJ_2D.get('data')
    #相关还款责任信息,出现次数[1..1],下级字段出现次数[0..*]
    EDD = getTable(data, 'EDD', 1, 0)
    if EDD == [{}]:
        return u'缺失值'
    elif EDD == 0:
        return 0
    else:
        #相关还款责任借贷账户（不含贴现）信息单元,出现次数[0..*]，下级字段出现次数[1..1]
        ED07 = getTable(EDD[0], 'ED07', 0, 1)
        # 相关还款责任贴现账户分机构汇总信息单元,出现次数[0..*]，下级字段出现次数[1..1]
        ED08 = getTable(EDD[0], 'ED08', 0, 1)
        #相关还款责任担保账户分机构汇总信息单元,出现次数[0..*]，下级字段出现次数[1..1]
        ED09 = getTable(EDD[0], 'ED09', 0, 1)
        
        if ED07 == [{}] and ED08 == [{}] and ED09 == [{}]:
            return u"缺失值"
        elif ED07 == 0 and ED08 == 0 and ED09 == 0:
            return 0
        else:
            conts = 0
            factor = 0
            for i in ED07:
                try:
                    #到期日期
                    ED070R02 = i.get('ED070R02')
                    #五级分类
                    ED070D08 = i.get('ED070D08')
                    if ED070R02 in null_type_list or ED070D08 in null_type_list or not isfloat(ED070D08) or not  parse(ED070R02): 
                        conts += 1
                    else:
                        if ED070D08 in ["2",2] and parse(ED070R02).date() >= datetime.datetime.now().date():
                            factor += 1
                        else:
                            pass
                except:
                    conts += 1
                    
            for i in ED08:
                try:
                    #账户数
                    ED080S01 = i.get('ED080S01')
                    #五级分类
                    ED080D04 = i.get('ED080D04')
                    if ED080S01 in null_type_list or ED080D04 in null_type_list or not isfloat(ED080S01) or not isfloat(ED080D04): 
                        conts += 1
                    else:
                        if ED080D04 in ["2",2]:
                            factor += float(ED080S01)
                        else:
                            pass
                except:
                    conts += 1
                    
            for i in ED09:
                try:
                    #账户数
                    ED090S01 = i.get('ED090S01')
                    #五级分类
                    ED090D04 = i.get('ED090D04')
                    #余额
                    ED090J02 = i.get('ED090J02')
                    if ED090S01 in null_type_list or ED090D04 in null_type_list or not isfloat(ED090S01) or not isfloat(ED090D04)\
                    or ED090J02 in null_type_list or not isfloat(ED090J02): 
                        conts += 1
                    else:
                        if ED090D04 in ["2",2] and float(ED090J02) > 0:
                            factor += float(ED090S01)
                        else:
                            pass
                except:
                    conts += 1
                    
            if conts == len(ED07) + len(ED08) + len(ED09):
                return u"缺失值"
            else:
                return int(factor)

result = EC88_EntCurrRelaGuaranteeConsAccCnt()
